package com.mlj.service;

import com.mlj.dao.DormitoryMapper;
import com.mlj.pojo.Admin;
import com.mlj.pojo.Dormitory;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DormitoryServiceImpl implements DormitoryService {
    @Autowired
    DormitoryMapper dormitoryMapper;

    @Override
    public List<Dormitory> getDormitoryName() {
        return dormitoryMapper.selectAll();
    }

    @Override
    public Result getDormitoryList(Integer page, Integer limit) {
        //计算分页查询的起始位置
        Integer start = (page-1)*limit;
        Result result = new Result();
        //分页查询列表
        List<Dormitory> dormitories = dormitoryMapper.findDormitoryListByPage(start,limit);
        result.setItem(dormitories);//获取数据
        Integer count = dormitoryMapper.selectCount();
        result.setTotal(count);//数据长度
        return result;
    }

    @Override
    public Result delDormitoryById(Integer id) {
        Result result = new Result();
        try{
            dormitoryMapper.delDormitoryById(id);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result deleteBatch(Integer[] ids) {
        Result result = new Result();
        try{
            dormitoryMapper.deleteBatch(ids);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result addHeader(Integer dormitoryId ,String dormitoryHeader) {
        Result result = new Result();
        try{
            dormitoryMapper.insertHeader(dormitoryId,dormitoryHeader);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(502);//自己编的响应码，代表错误
            result.setMessage("网络延迟，添加失败");
        }
        return result;
    }

    @Override
    public Result addDormitory(Dormitory dormitory) {
        Result result = new Result();
        try{
            dormitoryMapper.insertDormitory(dormitory);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(502);//自己编的响应码，代表错误
            result.setMessage("网络延迟，添加失败");
        }
        return result;
    }

    @Override
    public Dormitory selectDormitoryById(Integer dormitoryId) {

          return  dormitoryMapper.selectDormitoryById(dormitoryId);

    }

    @Override
    public Result updateDormitory(Dormitory dormitory) {
        Result result = new Result();
        try{
            dormitoryMapper.updateByPrimaryKey(dormitory);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，修改失败");
        }
        return result;
    }

    @Override
    public Result search(String name, Integer page, Integer limit) {
        Result result = new Result();
        //使用map存储传递的五个参数
        Map<String,Object> paraMaps = new HashMap<>();
        //计算分页查询起始位置
        Integer start = (page-1)*limit;
        paraMaps.put("start",start);
        paraMaps.put("limit",limit);
        paraMaps.put("name",name);
        //根据时间范围和关键字进行分页查询
        List<Admin> searchAdmins = dormitoryMapper.searchDormitoryByKw(paraMaps);
        result.setItem(searchAdmins);
        //搜索视频的长度,便于搜索之后分页
        Integer count = dormitoryMapper.searchCountByKw(paraMaps);
        result.setTotal(count);
        return result;
    }
}
